package com.sai.jsai.common.base.vo;

import com.sai.jsai.common.base.exception.CommonBizErrorCode;
import com.sai.jsai.common.base.exception.ErrorCode;
import lombok.Getter;
import lombok.Setter;

/**
 * @author jisai
 * @version 0.0.1
 * @Description TODO
 * @createTime 2024/09/21
 */
@Setter
@Getter
public class RestResult<T> {
    /**
     * 状态吗
     */
    private Integer code;

    /**
     * 是否成功
     */
    private Boolean success;

    /**
     * 消息描述
     */
    private String message;

    /**
     * 数据，可以是任何类型的VO
     */
    private T data;

    public RestResult() {
    }

    public RestResult(Boolean success, String message) {
        this.success = success;
        this.message = message;
    }

    public RestResult(Boolean success, Integer code, String message, T data) {
        this.success = success;
        this.message = message;
        this.data = data;
        this.code = code;
    }

    public static <T> RestResult<T> success(T data) {
        return new RestResult<>(true, CommonBizErrorCode.SUCCESS.getCode(), CommonBizErrorCode.SUCCESS.getMessage(), data);
    }

    public static <T> RestResult<T> success() {
        return success(null);
    }

    public static <T> RestResult<T> error(Integer errorCode, String errorMsg) {
        return new RestResult<>(false, errorCode, errorMsg, null);
    }

    public static <T> RestResult<T> error(ErrorCode errorCode) {
        return new RestResult<>(false, errorCode.getCode(), errorCode.getMessage(), null);
    }

}
